package com.qf.sy2103.springboot.Service.Impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import com.qf.sy2103.springboot.Dao.UseTableDao;
import com.qf.sy2103.springboot.Service.UseTableService;
import com.qf.sy2103.springboot.entity.UserTable;
import com.qf.sy2103.springboot.vo.Tablevo;
import com.qf.sy2103.springboot.vo.pagevo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class UseTableServiceImpl implements UseTableService {
    @SuppressWarnings("SpringJavaInjectionPointsAutowiringInspection")
    @Autowired
    private UseTableDao useTableDao;

    //获取分页数据展示的方法
    @Transactional(propagation = Propagation.SUPPORTS)
    @Override
    public Tablevo<UserTable> getAll(pagevo pagevo, UserTable userTable) {
        //使用pageHelper工具开启分页
        PageHelper.startPage(pagevo.getPage(),pagevo.getLimit());
        //获取数据库中的数据
//        List<UserTable> list = useTableDao.getAll();
        List<UserTable> lists = useTableDao.getusers(userTable);
        PageInfo<UserTable> objectPageInfo = new PageInfo<>(lists);
        return new Tablevo<>(0,"ok",objectPageInfo.getTotal(),lists);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public Boolean addUse(UserTable userTable) {
        int i  = useTableDao.addUse(userTable);
        if (i==0){
            return false;
        }
        return true;
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public Boolean updateUse(UserTable userTable) {
        int i = useTableDao.updateUse(userTable);
        if (i==0){
            return false;
        }
        return true;
    }

    //删除
    @Transactional(rollbackFor = Exception.class)
    @Override
    public Boolean deleteUse(List<Integer> ids) {
        int i  = useTableDao.deleteUse(ids);
        if (i==0){
            return false;
        }
        return true;
    }
}
